
-- 是否开启Debug相关的功能
-- 正式上线的时候将此值设置为false
_G['OpenDebug'] = IsInToolsMode()

-- 重载
local state = GameRules:State_Get()
if state > DOTA_GAMERULES_STATE_WAIT_FOR_PLAYERS_TO_LOAD then
	print("reload script")
	require("autoload")
end

-- 自动加载
_G['autoload'] = function (t,prefix)
	prefix = prefix or ""
	if prefix == "" then
		for i,v in ipairs(t) do
			require(v)
		end
	else
		prefix = prefix..'.'
		for i,v in ipairs(t) do
			require(prefix..v)
		end
	end
end

if CMyRpgGameMode == nil then
	CMyRpgGameMode = class({})
end

-- 载入系统各项资源
function Precache( context )
	--[[
		Precache things we know we'll use.  Possible file types include (but not limited to):
			PrecacheResource( "model", "*.vmdl", context )
			PrecacheResource( "soundfile", "*.vsndevts", context )
			PrecacheResource( "particle", "*.vpcf", context )
			PrecacheResource( "particle_folder", "particles/folder", context )
	]]

	-- 加载单位
	local units = LoadKeyValues("scripts/npc/npc_units_custom.txt")
	local finished = function() end
	for name in pairs(units) do
		PrecacheUnitByNameAsync(name, finished, -1)
	end

	-- 加载物品
	local Items = LoadKeyValues("scripts/npc/npc_items_custom.txt")
	for k, v in pairs(Items) do
		if k ~= "Version" then
			PrecacheItemByNameSync(k, context)
		end
	end
end

--自定义游戏脚本激活
function Activate()
	GameRules.MyRpg = CMyRpgGameMode()
	GameRules.MyRpg:InitGameMode()
end

--游戏脚本入口
function CMyRpgGameMode:InitGameMode()
	if OpenDebug then
		GameRules:GetGameModeEntity():SetContextThink(DoUniqueString("collectgarbage"), function()
			local m = collectgarbage('count')
			print(string.format("[Lua Memory]  %.3f KB  %.3f MB", m, m/1024))
			return 20
		end, 0)
	end

	-- 加载各个系统模块
	require('autoload')

	-- -- 定时清理垃圾
	GameRules:GetGameModeEntity():SetContextThink(DoUniqueString("collectgarbage"), function()
		collectgarbage('collect')
		return 600
	end, 600)

	--默认游戏难度等级为1，游戏速率为1
	CustomNetTables:SetTableValue("Common", "GameSetupInfoTable", {GameRateLevel=1,Difficulty=1} )
end